package com.j.Collection.Set;

import java.util.TreeSet;

/**
 * TreeSet
 * 基于排列顺序实现元素不重复
 * 实现了SortedSet的接口，对集合元素自动排序
 * 传入的对象必须实现Comparable接口，实现compareTo方法
 * 先进行compareTo方法的判断，根据方法的返回值进行排序
 * */
public class TestTreeSet {
    public static void main(String[] args) {
        Teacher teacher1=new Teacher("张三",13,"男",6000D);
        Teacher teacher2=new Teacher("李四",24,"女",5000D);
        Teacher teacher3=new Teacher("王五",15,"女",5000D);
        Teacher teacher4=new Teacher("赵六",16,"女",5000D);
        Teacher teacher5=new Teacher("刘能",19,"男",4000D);

        Teacher teacher6=new Teacher("张三",13,"男",6000D);

        TreeSet<Teacher> set=new TreeSet<Teacher>();
        set.add(teacher1);
        set.add(teacher2);
        set.add(teacher3);
        set.add(teacher4);
        set.add(teacher5);
        set.add(teacher6);

        for (Teacher ter :
                set) {
            System.out.println(ter);
        }
        System.out.println(teacher1.equals(teacher1));
    }
}
class Teacher implements Comparable<Teacher>{
    String name;
    Integer age;
    String sex;
    Double salary;

    public Teacher() {
    }

    public Teacher(String name, Integer age, String sex, Double salary) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", salary=" + salary +
                '}';
    }

    /**
     * 覆盖compareTo方法实现可排序对象
     * 如果此对象小于比较对象返回-1，大于比较对象返回+1，相同返回0
     *
     * */
    @Override
    public int compareTo(Teacher o) {
        //针对传入相同对象。equals比较地址
        if (this.equals(o)) return 0;
        //年龄大的靠后
        if (this.age>o.age){
            return 1;
        }else if(this.age<o.age){
            return -1;
        }else{
            //工资高的靠后
            if (this.salary>o.salary){
                return 1;
            }else if(this.salary<o.salary){
                return -1;
            }else{
                //名字根据字典排序
                if (this.name.compareTo(o.name)>0){
                    return 1;
                }else if(this.name.compareTo(o.name)<0){
                    return -1;
                }else{
                    return this.sex.compareTo(o.sex);
                }
            }
        }

    }
}